package com.zerosky.crm.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.NoticeDAO;
import com.zerosky.crm.po.Notice;
import com.zerosky.crm.service.NoticeService;
import com.zerosky.crm.util.PageInfo;

@Service(value="noticeService")
public class NoticeServiceImpl implements NoticeService {
	@Autowired
	private NoticeDAO noticeDAO; // Notice数据接口

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<Notice> getNoticeList(Map map) {
		return noticeDAO.getNoticeList(map);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public int deleteNotice(String id) {
		return noticeDAO.deleteNotice(id);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public int saveOrUpdate(Notice notice, String oper) {
		if (oper.equals("新增")) {
			return noticeDAO.saveNotice(notice);
		} else {
			return noticeDAO.updateNotice(notice);
		}

	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Notice> getPageInfoNoticeList(Map map, PageInfo<Notice> page) {
		if (map.get("method") == "1") {
			int begin = ((page.getPageNo() - 1) * page.getPageSize());
			int pageSize = page.getPageSize();

			// 写入数据
			map.put("pageNo", begin);
			map.put("pageSize", pageSize);
			List<Notice> list = noticeDAO.getNoticeList(map);
			page.setList(list);

			int count = noticeDAO.countNotice(map);// 获取总记录
			page.setPageCount(count);
		} else {
			map.put("pageNo", 0);
			map.put("pageSize", 6);
			List<Notice> list = noticeDAO.getNoticeList(map);
			page.setList(list);

			int count = noticeDAO.countNotice(map);// 获取总记录
			page.setPageCount(10);
		}

		return page;
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Notice getNoticeById(String id) {
		return noticeDAO.getNoticeById(id);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public int saveNoticeRelation(Map map) {
		return noticeDAO.saveNoticeRelation(map);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public int updateNoticeRelation(String noticeId) {
		return noticeDAO.updateNoticeRelation(noticeId);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public Integer getNewNotice(String userId) {
		return noticeDAO.getNewNotice(userId);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public Map<String, Object> updateNoticeStatus(Map<String, Object> map) {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		noticeDAO.updateNoticeStatus(map);
		json.put("code", 1);
		return json;
	}

}
